#include <iostream>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <string.h>
using namespace std;
const int mxN=1e5, bs=250;
int n, q, s[mxN], m[mxN], r[mxN], g[(mxN-1)/bs+1][mxN+3], lt1[(mxN-1)/bs+1], lt2[mxN], ti, li, ri;
long long ans;
void psh(int i) {
if(lt1[i]==-1) return;
for(int j=i*bs; j<min((i+1)*bs, n); ++j) lt2[j]=lt1[i];
lt1[i]=-1;
};
void ue(int i) {
ans+=min(s[i]+1ll*(ti-lt2[i])*r[i], 1ll*m[i]);
s[i]=0;
lt2[i]=ti;
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i=0; i<n; ++i) {
cin >> s[i] >> m[i] >> r[i];
if(!r[i])
continue;
g[i/bs][1]+=r[i];
g[i/bs][m[i]/r[i]+1]+=m[i]%r[i]-r[i];
g[i/bs][m[i]/r[i]+2]-=m[i]%r[i];
}
for(int i=0; i<=(n-1)/bs; ++i) {
lt1[i]=-1;
for(int j=2; j<=mxN; ++j)
g[i][j]+=g[i][j-1];
for(int j=2; j<=mxN; ++j)
g[i][j]+=g[i][j-1];
}
cin >> q;
while(q--) {
cin >> ti >> li >> ri, --li, --ri;
ans = 0;
psh(li/bs), psh(ri/bs);
for(; li%bs&&li<=ri; ++li)
ue(li);
for(; li+bs<=ri; li+=bs) {
if(lt1[li/bs]==-1)
for(int j=li; j<li+bs; ++j)
ue(j);
else
ans+=g[li/bs][min(ti-lt1[li/bs], mxN)];
lt1[li/bs]=ti;
}
for(; li<=ri; ++li)
ue(li);
cout << ans << "\n";
}
}
1612A - Distance | 520A - Pangram |
124A - The number of positions | 1041A - Heist |
901A - Hashing Trees | 1283A - Minutes Before the New Year |
1654D - Potion Brewing Class | 1107B - Digital root |
25A - IQ test | 785A - Anton and Polyhedrons |
1542B - Plus and Multiply | 306A - Candies |
1651C - Fault-tolerant Network | 870A - Search for Pretty Integers |
1174A - Ehab Fails to Be Thanos | 1169A - Circle Metro |
780C - Andryusha and Colored Balloons | 1153A - Serval and Bus |
1487C - Minimum Ties | 1136A - Nastya Is Reading a Book |
1353B - Two Arrays And Swaps | 1490E - Accidental Victory |
1335A - Candies and Two Sisters | 96B - Lucky Numbers (easy) |
1151B - Dima and a Bad XOR | 1435B - A New Technique |
1633A - Div 7 | 268A - Games |
1062B - Math | 1294C - Product of Three Numbers |